import {
	singleSaveRequest,
	deleteRequest,
	updateRequest,
	getListRequest
} from '../../api/driver/vehicle.js'
const state = {
	carList: [[],[],[]], // 所有车辆信息
	currentCar: {}, // 当前选择的车辆信息
	showCarInfoWindow: false, // 弹出框是否出现
	type: 'add' ,// 默认为新增模式
	currentTab: 0, // 当前选择的车辆卡片所属的tab标签
};

const mutations = {
	saveCar: (state, data) => {
		state.carList[1].unshift(data);
	},
	updateCar: (state, data) => {
		state.carList[state.currentTab].splice(state.carList[state.currentTab].findIndex(item => item.id === data.id), 1, data);
	},
	getCarListUseable: (state, {data,page,carState}) => {
		let index = 0;
		if(carState === 2) {
			index = 1;
		}else if(carState === 4 ){
			index = 0;
		}else {
			index = 2;
		}
		if(page === 1){
			state.carList[index].splice(0)
		}
		state.carList[index].push(...data);
	},
	getCarListReviwing: (state,{data,page}) => {
		if(page === 1){
			state.carList[1].splice(0)
			console.log(state.carList);
		}
		state.carList[1].push(...data);
	},
	deleteCar: (state,data) => {
		state.carList[0].splice(state.carList[0].findIndex(item => data.includes(item.id)),1);
	},
	clearCarList: state => {
		state.carList = [[],[],[]];
	},
	changeInfoWindow: (state,show) => {
		state.showCarInfoWindow = show;
	},
	changeCurrentCar: (state,{car,tab}) => {
		state.currentCar = car;
		state.currentTab = tab;
	},
	changeType: (state, type) => {
		state.type = type;
	}
};

const actions = {
	SAVECAR({commit}, data) {
		return new Promise((resolve, reject) => {
			singleSaveRequest({
					vehicle: JSON.stringify(data)
				})
				.then(res => {
					if (res.header.code === 200) {
						commit('saveCar', data);
					}
					resolve(res.header);
				})
				.catch(err => {
					reject(err);
				})
		});
	},
	DELETECAR({commit}, data) {
		return new Promise((resolve, reject) => {
			deleteRequest(data)
				.then(res => {
					commit('deleteCar',data);
					resolve(res.header);
				})
				.catch(err => {
					reject(err);
				})
		});
	},
	UPDATECAR({commit}, data) {
		return new Promise((resolve, reject) => {
			updateRequest({
					vehicle: JSON.stringify(data)
				})
				.then(res => {
					if (res.header.code === 200) {
						commit('updateCar', data);
					}
					resolve(res.header);
				})
				.catch(err => {
					reject(err);
				})
		});
	},
	GETCARLISTUSEABLE({commit}, data) {
		return new Promise((resolve, reject) => {
			getListRequest(data)
				.then(res => {
					if (res.header.code === 200) {
						commit('getCarListUseable', {data:res.body.list, page: data.page, carState: data.carState});
					}
					resolve(res.header);
				})
				.catch(err => {
					reject(err);
				})
		});
	},
	GETCARLISTREVIEWING({}, data){
		return new Promise((resolve, reject) => {
			getListRequest(data)
				.then(res => {
					if (res.header.code === 200) {
						commit('getCarListReviwing', {data:res.body.list, page: data.page});
					}
					resolve(res.header);
				})
				.catch(err => {
					reject(err);
				})
		});
	},
	CHANGECURRENTCAR({commit},{car,tab}) {
		return new Promise((resolve,reject) => {
			commit('changeCurrentCar',{car:car,tab:tab});
			resolve();
		});
		
	},
	CLEARCARLSIT({commit}) {
		commit('clearCarList');
	},
	CHANGEINFOWINDOW({commit},show) {
		commit('changeInfoWindow',show);
	},
	CHANGETYPE({commit},type) {
		commit('changeType',type);
	}
}

export default {
	namespaced: true,
	mutations,
	actions,
	state
}
